/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ocumed.teams.teamb.gui;

import java.util.HashMap;
import java.util.List;
import javax.swing.table.AbstractTableModel;

import ocumed.teams.teamb.application.PatientReihung;

/**
 *
 * @author apr3455
 */
public abstract class WarteschlangeModel extends AbstractTableModel {

    protected PatientReihung m_controller = null;
    protected String[] m_columnName = null;
    protected List<Integer> m_aerzteIds = null;
    protected HashMap<Integer, List<Integer>> m_warteschlangen = new HashMap<Integer, List<Integer>>();
    protected HashMap<Integer, String> m_patientNames = new HashMap<Integer, String>();
    protected Integer m_rowCount = 0;
    protected List<Integer> m_patientListe = null;
    
    public WarteschlangeModel(PatientReihung controller){
        m_controller = controller;
        buildTable();
    }
    
    abstract protected void initAerzte();
    
    abstract protected void initColumn();

    abstract protected Integer getSelectedPatient(Integer row, Integer col);
    
    abstract protected String getArztName(int arztId);

    abstract public int getColumnCount();
    
    abstract public Object getValueAt(int row, int col);
    
    abstract protected void setRowCount();
    
    
    public void buildTable() {
        
        clear();
        
        initAerzte();
        initColumn();
        
        update();
    }
    
    public void update(){
        clear();
        for(int i = 1; i < m_aerzteIds.size(); i++){
            m_columnName[i] = getArztName(i);
            m_patientListe = m_controller.getWarteschlange(m_aerzteIds.get(i));
            m_warteschlangen.put(m_aerzteIds.get(i), m_patientListe);
            if(m_patientListe != null){
                setRowCount();
                for(int e = 0; e < m_patientListe.size(); e++){
                    m_patientNames.put(m_patientListe.get(e), m_controller.getPatient(m_patientListe.get(e)));
                }
            }
        }
        
        for(int i = 0; i < getRowCount(); i++){
            for(int e = 1; e < getColumnCount(); e++){
                fireTableCellUpdated(i, e);
            }
        }
    }
    
    private void clear(){
        if(m_patientListe != null) m_patientListe.clear();
        m_warteschlangen.clear(); 
        m_patientNames.clear(); 
        m_rowCount = 0;
        
    }
    
    @Override
    public String getColumnName(int col) {
        return m_columnName[col];
    }
    
    public int getRowCount() {
        return m_rowCount;
    }
    
    protected Integer getPatientIdAt(int row, int col){
        if(row >= 0 && col >= 0 && col < m_aerzteIds.size() && m_warteschlangen.containsKey(m_aerzteIds.get(col)) == true){
            List<Integer> patientListe = m_warteschlangen.get(m_aerzteIds.get(col));
            if(patientListe != null && row < patientListe.size()){
                if(m_patientNames.containsKey(patientListe.get(row)) == true){
                    return patientListe.get(row);
                }
            }
        }
        return null;
    }
    
}
